package com.example.smartcanteen.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class JwtUtil {
private static final String SIGN="ZD";
    //生成token
    public static String createToken(String username,String password) {
        return JWT.create().withAudience(username)
                .withAudience(password)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 12))
                .sign(Algorithm.HMAC256(SIGN));
    }


    //验证token
    public static Boolean verifyToken(String token){
        DecodedJWT jwt;
        JWTVerifier verifier=JWT.require(Algorithm.HMAC256(SIGN)).build();
        jwt=verifier.verify(token);
        if (jwt.getExpiresAt().before(new Date())){
            System.out.println("token过期");
            return false;
        }
        return true;
    }
}
